package com.niuxiaofa.newlaw.Activity;


import android.util.Log;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.MemberFragmentRecordBean;
import com.niuxiaofa.newlaw.Network.ApiService;
import com.niuxiaofa.newlaw.Network.ServiceManager;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.RecyclerViewHelp;
import com.niuxiaofa.newlaw.adapter.BlindBoxRecordAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 盲盒记录
 */
public class BlindBoxRecordActivity extends BaseActivity {

    List<MemberFragmentRecordBean.DataDTO.RecordsDTO> newsList = new ArrayList<>();

    BlindBoxRecordAdapter blindBoxRecordAdapter;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_blind_box_record;
    }

    @Override
    protected void initData() {

        setTitle("碎片记录");

        blindBoxRecordAdapter = new BlindBoxRecordAdapter(newsList, new BlindBoxRecordAdapter.Onclick() {
            @Override
            public void onclick(int c) {

            }
        });

        RecyclerViewHelp.setRecycler(recyclerview, 0).setAdapter(blindBoxRecordAdapter);

        Map map = new HashMap();
        map.put("current", 1);
        map.put("size", 100);

        Gson gson = new Gson();
        String json = gson.toJson(map);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);

        Call<MemberFragmentRecordBean> call = ServiceManager.getInstance().creat(ApiService.class).getMemberFragmentRecord(body);
        call.enqueue(new Callback<MemberFragmentRecordBean>() {
            @Override
            public void onResponse(Call<MemberFragmentRecordBean> call, Response<MemberFragmentRecordBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    MemberFragmentRecordBean bean = (MemberFragmentRecordBean) response.body();
                    newsList.clear();
                    newsList.addAll(bean.getData().getRecords());
                }
            }

            @Override
            public void onFailure(Call<MemberFragmentRecordBean> call, Throwable t) {
                Log.e("链接错误", t.getMessage());
                //       Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });


    }
}
